//
//  MLZRegiserViewController.m
//  MotherLover
//
//  Created by lanou on 16/1/8.
//  Copyright © 2016年 zj. All rights reserved.
//

#import "MLZRegiserViewController.h"
#define MLR 10
#define MTB 10
@interface MLZRegiserViewController ()

@end

@implementation MLZRegiserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];
    [self addViewRegis];
}
- (void)addViewRegis{
//输入手机
    _textPhone = [[UITextField alloc]initWithFrame:CGRectMake(MLR, KH/4.5, (KW-MLR*3)/2, 30)];
    _textPhone.borderStyle = UITextBorderStyleRoundedRect;
    _textPhone.placeholder = @"请输入手机号码";
    [self.view addSubview:_textPhone];
 //输入验证码
    _textValidation = [[UITextField alloc]initWithFrame:CGRectMake(MLR, self.textPhone.bottom+MLR*2, (KW-MLR*3)/2, 30)];
    _textValidation.borderStyle = UITextBorderStyleRoundedRect;
    _textValidation.placeholder = @"请输入验证码";
    [self.view addSubview:_textValidation];
    //输入密码然后注册
    _password = [[UITextField alloc]initWithFrame:CGRectMake(MLR, self.textValidation.bottom+MLR*2, (KW-MLR*3)/2, 30)];
    _password.borderStyle = UITextBorderStyleRoundedRect;
    _password.placeholder = @"请输入密码";
    [self.view addSubview:_password];

    

    //发送验证码
    _buttonMessage = [UIButton buttonWithType:(UIButtonTypeCustom)];
  
    _buttonMessage.frame = CGRectMake(_textPhone.right+MLR, _textPhone.frame.origin.y, _textPhone.width , _textPhone.height);
    _buttonMessage.backgroundColor = [UIColor grayColor];
    [_buttonMessage.layer setCornerRadius:_buttonMessage.width/10];
    _buttonMessage.layer.masksToBounds= YES;
    [_buttonMessage addTarget:self action:@selector(buttonMessageAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [_buttonMessage setTitle:@"发送验证码" forState:(UIControlStateNormal) ];
    [self.view addSubview:_buttonMessage];
    
    //验证验证码
    _buttonValidation = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    _buttonValidation.frame = CGRectMake(_textPhone.right+MLR, _buttonMessage.bottom+MTB*2, _textPhone.width , _textPhone.height);
    _buttonValidation.backgroundColor = [UIColor grayColor];
    [_buttonValidation.layer setCornerRadius:_buttonMessage.width/10];
    _buttonValidation.layer.masksToBounds= YES;
    [_buttonValidation addTarget:self action:@selector(buttonValidationAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [_buttonValidation setTitle:@"验证验证码" forState:(UIControlStateNormal) ];
    [self.view addSubview:_buttonValidation];
    
    //注册
    
    _buttonRegis = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    _buttonRegis.frame = CGRectMake(MLR+((KW-MLR*2)/2)/2, _password.bottom+MTB*2, _textPhone.width , _textPhone.height);
    _buttonRegis.backgroundColor = [UIColor greenColor];
    [_buttonRegis.layer setCornerRadius:_buttonMessage.width/10];
    _buttonRegis.layer.masksToBounds= YES;
    [_buttonRegis addTarget:self action:@selector(buttonRegisAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [_buttonRegis setTitle:@"注册" forState:(UIControlStateNormal) ];
    [self.view addSubview:_buttonRegis];

    


}
-(void)buttonRegisAction:(UIButton*)button{
    
    [BmobUser signOrLoginInbackgroundWithMobilePhoneNumber:_textPhone.text SMSCode:_textValidation.text andPassword:_password.text block:^(BmobUser *user, NSError *error) {
        NSLog(@"%@,%@",user,error);
        
        if(!error){
        
        
        
        
        
        }
        
        
        
        
    }];
    
  
}
//发送验证码
-(void)buttonMessageAction:(UIButton*)button{

    [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:_textPhone.text andTemplate:@"5555555" resultBlock:^(int number, NSError *error) {
                 NSLog(@"%d,%@",number,error);
        
        
        
        
        
        }];

   


}
//验证验证码
- (void)buttonValidationAction:(UIButton*)button{


[BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:_textPhone.text andSMSCode:_textValidation.text resultBlock:^(BOOL isSuccessful, NSError *error) {
    NSLog(@"%d",isSuccessful);
    
    if(isSuccessful){
        
        [self alertActionWithTitle:@"验证成功" message:@"" other:@"确定"];
        
        
    }else {
    
    
        [self alertActionWithTitle:@"验证错误" message:@"重新发送" other:@"确定"];

    
    }
    
    
    
    
}];


    
    
    

}
//提示界面
- (UIAlertController *)alertActionWithTitle:(NSString *)title message:(NSString *)message other:(NSString *)other
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:other style:UIAlertActionStyleDefault handler:nil];
    [alertVc addAction:otherAction];
    [self presentViewController:alertVc animated:YES completion:nil];
    return alertVc;
}

    
    
    
    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
